package cn.yunhe.controller;

import cn.yunhe.dao.InsurCateDao;
import cn.yunhe.dao.InsurcatetocomboDao;
import cn.yunhe.entity.Combo;
import cn.yunhe.entity.Insurcate;
import cn.yunhe.service.ComboService;
import cn.yunhe.service.InsurcateService;
import cn.yunhe.service.InsurcatetocombService;
import cn.yunhe.util.Result;
import cn.yunhe.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 董稳钰
 * @date 2018/9/20 20:18
 */
@RestController
@RequestMapping("/comb")
public class CombController {
    @Resource
    private ComboService comboService;
    @Resource
    private InsurcatetocombService insurcatetocombService;
    @Resource
    private InsurcateService insurcateService;

    /**
     * 添加套餐
     *
     * @param
     * @return
     */
    @RequestMapping("/toAddCombo")
    public ModelAndView toAddCombo() {
        return new ModelAndView("combo_add");
    }

    @RequestMapping("/toComboList")
    public ModelAndView toComboList() {
        return new ModelAndView("combo_list");
    }

    @RequestMapping("/toUpdateCombo")
    public ModelAndView toUpdateCombo(String comuuid) {
        Combo combo1 = comboService.getComboById(comuuid);

        ModelAndView mv = new ModelAndView("combo_update");
        mv.addObject("combo1", combo1);
        return mv;
    }

    @RequestMapping("/addComb")
    public ModelAndView delsEmp(@RequestParam(value = "iuuIdList[]") String[] iuuIdList, @RequestParam(value = "insurname") String insurance, @RequestParam(value = "insuratio") double insuratio) {
        Combo combo = new Combo();
        String comuuid = UUIDGenerator.getUUID();
        combo.setComuuid(comuuid);
        combo.setCombname(insurance);
        combo.setCombrate(insuratio);
        comboService.addCombo(combo);
        List<String> iuuId = Arrays.asList(iuuIdList);
        String[] iuuids = new String[iuuId.size()];
        iuuId.toArray(iuuids);
        /*int[]arry=new int[iuuId.size()];*/
        Map map = new HashMap();
        for (int i = 0; i < iuuId.size(); i++) {
            map.put("comuuid", comuuid);
            map.put("iuuid", iuuids[i]);
            insurcatetocombService.addInsurtoComb(map);
        }
        return new ModelAndView("combo_list");
    }

    @RequestMapping("/toGetComb")
    public Map<String, Object> toGetComb(int pageSize, int pageNum, String keyword) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("keyword", keyword);
        Page page = comboService.findLikeCombos(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    //删除套餐
    @RequestMapping("/delComb")
    public Result delCombo(String comuuid) {
        comboService.delCombo(comuuid);
        return new Result();
    }
    //批量删除套餐
    @RequestMapping("/delCombos")
    public ModelAndView delCombos(@RequestParam(value="comuuidList[]") String[] comuuidList){
        List<String>combos=Arrays.asList(comuuidList);
        insurcateService.delInsurcates(combos);
        return new ModelAndView("combo_list");
    }
    //修改套餐
    @RequestMapping("/updateCombo")
    public Result updateComb(Combo combo) {
        comboService.updateCombo(combo);
        return new Result();
    }

    public ComboService getComboService() {
        return comboService;
    }

    public void setComboService(ComboService comboService){
        this.comboService = comboService;

    }

    public InsurcatetocombService getInsurcatetocombService() {
        return insurcatetocombService;
    }

    public void setInsurcatetocombService(InsurcatetocombService insurcatetocombService) {
        this.insurcatetocombService = insurcatetocombService;
    }

    public InsurcateService getInsurcateService() {
        return insurcateService;
    }

    public void setInsurcateService(InsurcateService insurcateService) {
        this.insurcateService = insurcateService;
    }
}
